//
//  UIButton+Extension.m
//  CH999
//
//  Created by zY on 17/5/19.
//  Copyright © 2017年 ch999.com. All rights reserved.
//

#import "UIButton+Extension.h"

@implementation UIButton (Extension)

- (void)setButtonLayout:(CHButtonLayout)layout offset:(CGFloat)offset
{
    [self layoutIfNeeded];
    
    CGSize imageSize = self.imageView.bounds.size;
    CGSize titleSize = self.titleLabel.bounds.size;
    
    switch (layout) {
        case CHButtonLayoutTitleLeft:
        {
            self.imageEdgeInsets = (UIEdgeInsets){0,titleSize.width + offset, 0, -(titleSize.width + offset)};
            self.titleEdgeInsets = (UIEdgeInsets){0, -(imageSize.width + offset), 0, imageSize.width + offset};
        }
            break;
        case CHButtonLayoutTitleRight:
        {
            self.imageEdgeInsets = (UIEdgeInsets){0,-offset, 0, offset};
            self.titleEdgeInsets = (UIEdgeInsets){0, offset, 0, -offset};
        }
            break;
        case CHButtonLayoutTitleTop:
        {
            self.imageEdgeInsets = (UIEdgeInsets){(titleSize.height + offset)/2, titleSize.width/2, -(titleSize.height + offset)/2, -titleSize.width/2};
            self.titleEdgeInsets = (UIEdgeInsets){-(imageSize.height + offset), -imageSize.width/2, imageSize.height + offset, imageSize.width/2};
        }
            break;
        case CHButtonLayoutTitleBottom:
        {
//            self.imageEdgeInsets = (UIEdgeInsets){-(imageSize.height + offset), titleSize.width/2, imageSize.height + offset - 30, -titleSize.width/2};
//            self.titleEdgeInsets = (UIEdgeInsets){(titleSize.height + offset)/2, -imageSize.width/2, -(titleSize.height + offset)/2 + offset - 30, imageSize.width/2};
            self.titleEdgeInsets = UIEdgeInsetsMake(self.imageView.frame.size.height, -self.imageView.bounds.size.width, 0,0);
            // button图片的偏移量
            self.imageEdgeInsets = UIEdgeInsetsMake(-10, self.titleLabel.frame.size.width/2, self.titleLabel.frame.size.height+5, -self.titleLabel.frame.size.width/2);
        }
            break;
        default:
            break;
    }
}



@end
